<?php
include_once 'mod.php';

session_start();
$id = $_GET['id'];
if (empty($_GET['id'])){

}

$mod = new mod();
$mysql = $mod->link();

$sql = 'select * from category';
$result = $mod->select($mysql,$sql);
if($_SERVER['REQUEST_METHOD']=='POST'){
    $id = $_SESSION['id'];
    if(empty($_POST['cid'])){
        echo '没有选择分类<a href="update.php">重新添加</a>';die;
    }
    if(empty($_POST['name'])){
        echo '没有输入名称<a href="update.php">重新添加</a>';die;
    }
    if(empty($_POST['price'])){
        echo '没有选输入价格<a href="update.php">重新添加</a>';die;
    }
    if(empty($_POST['stock'])){
        echo '没有输入库存<a href="update.php">重新添加</a>';die;
    }
    if(!isset($_POST['status'])){
        echo '没有选择状态<a href="update.php">重新添加</a>';die;
    }
    if(!isset($_POST['id'])){
        echo '没有id参数<a href="update.php">重新添加</a>';die;
    }

    $name = $_POST['name'];
    $cid = $_POST['cid'];
    $status = $_POST['status'];
    $price = $_POST['price'];
    $info = '';
    $picture = '';
    $update_time = date('Y-m-d H:i:s');
    $stock = $_POST['stock'];
    $desc = $_POST['desc'];


    $sql = "update products set `name`= '$name' ,`price`= $price,
                 info= '$info',picture= '$picture',update_time= '$update_time',stock= $stock,
                 `desc`= '$desc' where id= $id   ";
    $result = $mod->IDU($mysql,$sql);
    if($result['flag']){
        echo "修改成功<a href='index.php'>返回首页</a>";die();
    }else{
        echo $sql;
        echo "修改失败({$result['msg']})<a href='index.php'>返回首页</a>";die();
    }


}

if (empty($_GET['id'])){
    die('没有id参数<a href="index.php">返回首页</a>');
}
$_SESSION['id'] = $id;
$sql = "select * from products where id= $id";
$productInfo = $mod->select($mysql,$sql);
if(empty($productInfo)){
    echo '找不到该产品';die;
}
$productInfo = $productInfo[0];
?>


<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>修改产品</title>
</head>
<body>
   <form action="update.php" method="post">
       <p>分类：
           <select name="cid">
               <option value="">请选择分类</option>
               <?php foreach ($result as $item){?>
                   <option value="<?= $item['id']?>"   <?=  $productInfo['cid']==$item['id']  ? 'selected'  : '' ?>    ><?= $item['name']?></option>
               <?}?>

           </select>
       </p>
       <p>名称： <input type="text" name="name" value="<?= $productInfo['name']?>"></p>
       <p> 价格：<input type="text" name="price" value="<?= $productInfo['price']?>"></p>
       <p>库存:<input type="text" name="stock" value="<?= $productInfo['stock']?>"></p>
       <p>状态:
           <input type="radio" value="1" name="status" <?= $productInfo['status']==1  ? 'checked' : '' ?>>上架
           <input type="radio"  value="0" name="status" <?= $productInfo['status']==0  ? 'checked' : '' ?>>下架
       </p>
       <p>描述:
        <textarea  name="desc"><?= $productInfo['desc']?></textarea>
       </p>
       <input type="hidden" name="id" value="<?= $productInfo['id']?>">

       <button type="submit">修改</button>


   </form>
</body>
</html>
